import groovy.mock.interceptor.*
import grails.test.GrailsUnitTestCase

class BookmarkControllerTests extends GroovyTestCase {

	BookmarkController bc
	final Closure UPDATE_TEST_CODE = { id ->
		bc = new BookmarkController()
		bc.params.id = id
		bc.update.call()
	}

    void main(String[] strs) {
        testSortBookmarks()
    }
	
	void setUp() {
		new Bookmark( url: new URL("http://www.google.com"),
                title: "Google",
                dateCreated: new Date(),
                rating:1,
                type:'blog',
                notes: "--sin notas--").save()

		new Bookmark( url: new URL("http://www.clarin.com"),
                title: "Clarin",
                dateCreated: new Date(),
                rating:8,
                type:'news',
                notes: "--sin notas--").save()

		new Bookmark( url: new URL("http://www.yahoo.com"),
                title: "Yahoo",
                dateCreated: new Date(),
                rating:3,
                type:'blog',
                notes: "--sin notas--").save()

        new Bookmark( url: new URL("http://www.taringa.net"),
                title: "Taringa",
                dateCreated: new Date(),
                rating:5,
                type:'general',
                notes: "--sin notas--").save()
		new Bookmark( url: new URL("http://www.grails.org"),
                title: "Grails",
                dateCreated: new Date(),
                rating:6,
                type:'news',
                notes: "--sin notas--").save()

	}

	void testSortBookmarks() {
     
		Bookmark.list().size()

		def sortUrlsBy = { property ->
			Bookmark.findAll().sort { it."$property" }.url
		}
		
		def urls = sortUrlsBy("title")

		assert urls			
		assert urls.size() != 6
		assert urls.size() == 5
		assert urls[0].toString() == "http://www.clarin.com"
						
  	}

	void testListAction(){
		def bc = new BookmarkController()
		def bookmarks = bc.list()?.bookmarkList
		
		assert bookmarks != null
		assertLength( 5, bookmarks as Object[] )
		assert bookmarks[0].url?.toString() == "http://www.google.com"
	}

	void testListActionPaginate(){
		def bc = new BookmarkController()
		bc.params.offset = 0
		bc.params.max = 2
		
		def bookmarks = bc.list()?.bookmarkList

		assert bookmarks != null
		assertLength(2, bookmarks as Object[])
		println bookmarks[0].url?.toString() 
		println bookmarks[1].url?.toString() 
		assert bookmarks[0].url?.toString() == "http://www.google.com"	
		assert bookmarks[1].url?.toString() == "http://www.clarin.com"
		
		bc.params.offset = 2
		bc.params.max = 2
		bookmarks  = bc.list()?.bookmarkList

		println bookmarks[0].url?.toString() 
		println bookmarks[1].url?.toString() 
		assert bookmarks != null
		assertLength(2, bookmarks as Object[])
		assert bookmarks[0].url?.toString() == "http://www.yahoo.com"	
		assert bookmarks[1].url?.toString() == "http://www.taringa.net"
	}

	void testBadUrl() {
		def badUrl = "ani_invalid_url"
		def b = new Bookmark()
		shouldFail(MalformedURLException) {
			b.url = new URL( badUrl )
		}
	}

	
	/*void testUpdateNotFound() {
		def mock = new MockFor(BookmarkController)
		mock.demand.redirect { Map params ->
			assert params.action == bc.edit
		}
		mock.use(UPDATE_TEST_CODE.curry(5))
	}

	void testUpdateSuccess() {
		def ctrlMock = new MockFor(BookmarkController)
		def bookmarkStub = new StubFor(Bookmark)
		def grailsBookmark= Bookmark.findByTitle('Grails')

		bookmarkStub.demand.get { grailsBookmark }
		ctrlMock.demand.redirect { Map params ->
			assert params.action == bc.show
			assert params.id == grailsBookmark?.id
		}
		ctrlMock.use {
			bookmarkStub.use(UPDATE_TEST_CODE.curry(grailsBookmark?.id))
		}
	}

	void testUpdateFailure() {
		def ctrlMock = new MockFor(BookmarkController)
		def bookmarkStub = new MockFor(Bookmark)
		def grailsBookmark = Bookmark.findByTitle('Grails')
		bookmarkStub.demand.get {
			grailsBookmark.title = null
			return grailsBookmark
		}
	
		ctrlMock.demand.render { Map params ->
			assert params.view == 'edit'
			assert params.model?.bookmark == grailsBookmark
		}
		ctrlMock.use {
			bookmarkStub.use(UPDATE_TEST_CODE.curry(grailsBookmark.id))
		}
	}
*/
	void tearDown(){	
 		//Bookmark.list().each { bookmark ->
        	//   println "id:-${bookmark.id}-"
	        //    Bookmark.get(bookmark.id).delete()
        	//}
		Bookmark.list()*.delete()
	}

}
